/*
 * @Author: i@rysa.cn
 * @Date: 2022-09-14 12:34:12
 * @LastEditTime: 2022-09-14 13:06:36
 * @LastEditors: i@rysa.cn
 * @Description:
 * @FilePath: /go-utils/wechat/pay/transfer_batch.go
 */
package pay

import (
	"github.com/wechatpay-apiv3/wechatpay-go/core"
	"github.com/wechatpay-apiv3/wechatpay-go/services/transferbatch"
)

type transferBatch struct {
	*PayConfig
}

type BatchData struct {
	BatchName          string  `json:"batch_name"`
	BatchRemark        string  `json:"batch_remark"`
	OutBatchNo         string  `json:"out_batch_no"`
	TotalAmount        float64 `json:"total_amount"`
	TotalNum           int64   `json:"total_num"`
	TransferDetailList []transferbatch.TransferDetailInput
}

func NewTransferBatch(cfg *PayConfig) *transferBatch {
	return &transferBatch{cfg}
}

func (p *transferBatch) Transfer(data BatchData) (*transferbatch.InitiateBatchTransferResponse, *core.APIResult, error) {
	client, ctx, err := GetClient(p.PayConfig)
	if err != nil {
		return nil, nil, err
	}
	svc := transferbatch.TransferBatchApiService{Client: client}
	resp, result, err := svc.InitiateBatchTransfer(ctx, transferbatch.InitiateBatchTransferRequest{
		Appid:              core.String(p.Appid),
		BatchName:          core.String(data.BatchName),
		BatchRemark:        core.String(data.BatchRemark),
		OutBatchNo:         core.String(data.OutBatchNo),
		TotalAmount:        core.Int64(int64(data.TotalAmount * 100)),
		TotalNum:           core.Int64(data.TotalNum),
		TransferDetailList: data.TransferDetailList,
	})
	return resp, result, err
}
